package com.sky.controller.User;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result saveShoppingCart(@RequestBody ShoppingCartDTO dto) {
        log.info("接收到前端传过来的添加购物车的数据为:{}", dto);
        shoppingCartService.addShoppingCart(dto);
        return Result.success();
    }

    /**
     * 查询当前用户的购物车列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        log.info("查找全部购物车商品数据-----------");
        List<ShoppingCart> shoppingCart = shoppingCartService.list();
        return Result.success(shoppingCart);
    }

    /**
     * 删除用户购物车的一个商品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    public Result deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("接收到前端传过来的要删除的购物车数据为:{}",shoppingCartDTO);
        shoppingCartService.deleteOne(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 删除用户购物车的所有商品
     * @return
     */
    @DeleteMapping("/clean")
    public Result deleteAll(){
        log.info("删除用户购物车的所有商品----------");
        shoppingCartService.deleteAll();
        return Result.success();
    }
}
